package com.itheima.youdianda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.youdianda.dto.PageDto;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.youdianda.domain.SysRole;
import com.itheima.youdianda.domain.SysRoleMenu;
import com.itheima.youdianda.dto.SysRoleDto;
import com.itheima.youdianda.mapper.SysRoleMapper;
import com.itheima.youdianda.service.SysRoleMenuService;
import com.itheima.youdianda.service.SysRoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName SysRoleServiceImpl
 * @Description
 * @Author Young
 * @Date 2023/2/12 17:06
 * @Version 1.0
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
    @Autowired
    private SysRoleMenuService roleMenuService;

    /**
     * 添加角色
     * @Author: Pan Rundong
     * @Date: 2023-02-13 20:04
     * @Greet2u:  （O｀ 3′O）~~♡
     * @param sysRoleDto
     * @return void
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void saveByMenus(SysRoleDto sysRoleDto) {
        //1.添加角色
        this.save(sysRoleDto);

        //2.添加role—Menu表
        List<String> menuIds = sysRoleDto.getMenuids();
        menuIds.stream().forEach((item)->{
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setMenuId(item);
            sysRoleMenu.setRoleId(sysRoleDto.getId());
            roleMenuService.save(sysRoleMenu);
        });
    }

    /**
     * 删除角色
     * @Author: Pan Rundong
     * @Date: 2023-02-13 20:38
     * @Greet2u:  （O｀ 3′O）~~♡
     * @param id
     * @return void
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void deleteByMenus(String id) {
        //1.删除自身
        LambdaQueryWrapper<SysRole> queryWrapperRole = new LambdaQueryWrapper<>();
        queryWrapperRole.isNull(SysRole::getDefaultData);
        queryWrapperRole.eq(SysRole::getId, id);
        this.remove(queryWrapperRole);

        //2.设置过滤器
        LambdaQueryWrapper<SysRoleMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRoleMenu::getRoleId, id);
        roleMenuService.remove(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void updateByMenu(SysRoleDto sysRoleDto) {
        this.updateById(sysRoleDto);

        LambdaQueryWrapper<SysRoleMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRoleMenu::getRoleId, sysRoleDto.getId());
        //查询
        roleMenuService.remove(queryWrapper);

        //添加
        List<String> menuids = sysRoleDto.getMenuids();
        menuids.stream().forEach((item)->{
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRoleId(sysRoleDto.getId());
            sysRoleMenu.setMenuId(item);
            roleMenuService.save(sysRoleMenu);
        });

    }

    /**
     * 角色分页查询
     * @Author: Pan Rundong
     * @Date: 2023-02-14 20:13
     * @Greet2u:  （O｀ 3′O）~~♡
     * @param page
     * @param limit
     * @param name
     * @return com.itheima.youdianda.dto.PageDto<com.itheima.youdianda.dto.SysRoleDto>
     */
    @Override
    public PageDto<SysRoleDto> getPage(int page, int limit, String name) {
        //1.设置条件过滤器
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, SysRole::getName, name);
        queryWrapper.eq(SysRole::getDelFlag, 0);
        Page<SysRole> pageInfo = new Page<>(page,limit);
        //2.查询列表
        this.page(pageInfo, queryWrapper);

        //3.根据
        Page<SysRoleDto> dtoPage = new Page<>();
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");

        List<SysRoleDto> dtoList = pageInfo.getRecords().stream().map((item) -> {
            SysRoleDto sysRoleDto = new SysRoleDto();
            //复制
            BeanUtils.copyProperties(item, sysRoleDto);
            //查询menuList
            String id = item.getId();
            LambdaQueryWrapper<SysRoleMenu> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(SysRoleMenu::getRoleId, id);
            queryWrapper1.orderByAsc(SysRoleMenu::getMenuId);

            List<SysRoleMenu> list = roleMenuService.list(queryWrapper1);
            List<String> menuIds = list.stream().map((item2) -> {
                return item2.getMenuId();
            }).collect(Collectors.toList());


            sysRoleDto.setMenuids(menuIds);
            return sysRoleDto;
        }).collect(Collectors.toList());

        dtoPage.setRecords(dtoList);

        //3.设置分页查询Dto
        PageDto<SysRoleDto> pageDto = new PageDto<>();
        BeanUtils.copyProperties(dtoPage,pageDto);

        pageDto.setValue(dtoPage);

        return pageDto;
    }
}
